package com.leyou.item.web;


import com.leyou.common.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listByids(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(this.skuService.listByids(ids));
    }

    /**
     * 分页查询spu
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ) {
        return ResponseEntity.ok(this.spuService.queryByPage(page, rows, saleable, categoryId, brandId, id));

    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(skuService.querySkuBySpuId(id));
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(spuDetailService.queryDetailById(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     *
     * @param id spu的id
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(spuService.querySpecValues(id, searching));
    }

    /**
     * 修改商品上下架
     *
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoods(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(this.spuService.queryGoods(spuId));
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuBuId(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.querySpuBuId(id));
    }

    @PostMapping("spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
